1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Linq;
7 using
System.Text;
8 using
System.Windows.Forms;
9 using
System.Data.SqlClient;
10 namespace
WarehouseManagementSystem
11 {
12     
public partial class frmCategoryRecord : Form
13     {
14         SqlDataReader rdr =
null;
15         SqlConnection con =
null;
16         SqlCommand cmd =
null;
17         ConnectionString cs =
new ConnectionString();
18         
public frmCategoryRecord()
19         {
20             InitializeComponent();
21         }
22
23         
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
24         {
25             
string strRowNumber = (e.RowIndex + 1).ToString();
26             SizeF size = e.Graphics.MeasureString(strRowNumber,
this.Font);
27             
if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
28             {
29                 dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width +
20));
30             }
31             Brush b = SystemBrushes.ControlText;
32             e.Graphics.DrawString(strRowNumber,
this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
33     
34         }
35
36         
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
37         {
38             DataGridViewRow dr = dataGridView1.SelectedRows[
0];
39             
this.Hide();
40             frmCategory frm =
new frmCategory();
41             
// or simply use column name instead of index
42             
//dr.Cells["id"].Value.ToString();
43             frm.Show();
44             frm.txtID.Text = dr.Cells[
0].Value.ToString();
45             frm.txtCategoryName.Text = dr.Cells[
1].Value.ToString();
46             frm.btnDelete.Enabled =
true;
47             frm.btnUpdate.Enabled =
true;
48             frm.txtCategoryName.Focus();
49             frm.btnSave.Enabled =
false;
50         }
51         
public void GetData()
52         {
53             
try
54             {
55                 con =
new SqlConnection(cs.DBConn);
56                 con.Open();
57                 String sql =
"SELECT * from Category order by CategoryName";
58                 cmd =
new SqlCommand(sql, con);
59                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
60                 dataGridView1.Rows.Clear();
61                 
while (rdr.Read() == true)
62                 {
63                     dataGridView1.Rows.Add(rdr[
0],rdr[1]);
64                 }
65                 con.Close();
66             }
67             
catch (Exception ex)
68             {
69                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
70             }
71         }
72
73         
private void frmCategoryRecord_FormClosing(object sender, FormClosingEventArgs e)
74         {
75             
this.Hide();
76             frmCategory frm =
new frmCategory();
77             frm.Show();
78         }
79
80         
private void frmCategoryRecord_Load(object sender, EventArgs e)
81         {
82             GetData();
83         }
84     }
85
86 }


Gõ tìm kiếm nhanh...